import folium
from folium.plugins import MarkerCluster
import webbrowser
import os


def create_car_manufacturer_map():
    # 简化的汽车厂商数据（包含近十年热销车型）
    car_brands = [
        {
            "name": "丰田",
            "country": "日本",
            "countryCode": "jp",
            "color": "#F53F3F",
            "location": [35.1495, 136.9151],
            "hotModels": ["卡罗拉", "凯美瑞", "RAV4"],
            "marketShare": 11.1
        },
        {
            "name": "大众",
            "country": "德国",
            "countryCode": "de",
            "color": "#1D2129",
            "location": [52.4374, 10.7953],
            "hotModels": ["高尔夫", "帕萨特", "途观"],
            "marketShare": 9.9
        },
        {
            "name": "本田",
            "country": "日本",
            "countryCode": "jp",
            "color": "#F53F3F",
            "location": [35.6762, 139.6503],
            "hotModels": ["思域", "雅阁", "CR-V"],
            "marketShare": 4.8
        },
        {
            "name": "奔驰",
            "country": "德国",
            "countryCode": "de",
            "color": "#1D2129",
            "location": [48.7758, 9.1829],
            "hotModels": ["C级", "E级", "S级"],
            "marketShare": 2.8
        },
        {
            "name": "宝马",
            "country": "德国",
            "countryCode": "de",
            "color": "#1D2129",
            "location": [48.1351, 11.5820],
            "hotModels": ["3系", "5系", "X5"],
            "marketShare": 2.8
        },
        {
            "name": "奥迪",
            "country": "德国",
            "countryCode": "de",
            "color": "#1D2129",
            "location": [48.7667, 11.4222],
            "hotModels": ["A4", "A6", "Q5"],
            "marketShare": 2.0
        },
        {
            "name": "比亚迪",
            "country": "中国",
            "countryCode": "cn",
            "color": "#F7BA1E",
            "location": [22.5431, 114.0579],
            "hotModels": ["秦", "汉", "宋"],
            "marketShare": 3.8
        },
        {
            "name": "特斯拉",
            "country": "美国",
            "countryCode": "us",
            "color": "#165DFF",
            "location": [30.2672, -97.7431],
            "hotModels": ["Model 3", "Model Y", "Model S"],
            "marketShare": 2.1
        },
        {
            "name": "现代",
            "country": "韩国",
            "countryCode": "kr",
            "color": "#722ED1",
            "location": [37.5665, 126.9780],
            "hotModels": ["伊兰特", "索纳塔", "途胜"],
            "marketShare": 4.6
        },
        {
            "name": "福特",
            "country": "美国",
            "countryCode": "us",
            "color": "#165DFF",
            "location": [42.3222, -83.1767],
            "hotModels": ["福克斯", "蒙迪欧", "F-150"],
            "marketShare": 4.4
        }
    ]

    # 创建地图对象，设置初始视图
    world_map = folium.Map(location=[20, 0], zoom_start=2, tiles="CartoDB positron")

    # 创建国家分组
    country_groups = {}

    # 添加标记到相应的国家分组
    for brand in car_brands:
        country_code = brand["countryCode"]

        # 如果国家组不存在，创建一个新的
        if country_code not in country_groups:
            country_groups[country_code] = folium.FeatureGroup(name=f"{brand['country']}厂商")
            world_map.add_child(country_groups[country_code])

        # 计算标记大小（基于市场份额）
        marker_size = brand["marketShare"] * 2

        # 创建弹出信息
        popup_html = f"""
            <div style="font-family: Arial, sans-serif; width: 220px;">
                <h3 style="color: {brand['color']}; margin-bottom: 5px;">{brand['name']}</h3>
                <p style="margin: 2px 0; font-size: 14px;"><strong>国家:</strong> {brand['country']}</p>
                <p style="margin: 2px 0; font-size: 14px;"><strong>近十年热销车型:</strong><br>
                {'<br>'.join(brand['hotModels'])}</p>
                <p style="margin: 8px 0 2px 0; font-size: 14px;"><strong>市场份额:</strong> {brand['marketShare']}%</p>
            </div>
        """

        # 创建标记并添加到对应组
        folium.CircleMarker(
            location=brand["location"],
            radius=marker_size,
            color=brand["color"],
            fill=True,
            fill_color=brand["color"],
            fill_opacity=0.7,
            popup=folium.Popup(popup_html, max_width=300),
            tooltip=brand["name"]
        ).add_to(country_groups[country_code])

    # 添加图层控制
    folium.LayerControl().add_to(world_map)

    # 保存地图为HTML文件
    map_file = "car_manufacturer_map_simplified.html"
    world_map.save(map_file)

    # 打开地图文件
    print(f"地图已成功生成，保存在: {os.path.abspath(map_file)}")

    # 在浏览器中打开地图
    webbrowser.open("file://" + os.path.abspath(map_file))


if __name__ == "__main__":
    create_car_manufacturer_map()